<?php
/**
 * MSSQL数据库操作类
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CMssql.php 2010-12-17 12:56:37Z andery $
 */
class CMssql implements Ischeme
{	
	protected $version = '';
	protected $_link_id = NULL;
	private $_query_count = 0;
	
	/**
	* 构造函数 实现链接数据库功能
	* @access private
	* @param array
	* @return string
	*/
	public function __construct($config) {
		$db_url = parse_url($config['db_url']);
    	if (empty($db_url['pass'])) {
	        $db_url['pass'] = '';
	    } else {
	        $db_url['pass'] = urldecode($db_url['pass']);
	    }
	    $db_url ['user'] = urldecode($db_url['user']);
	
	    if (empty($db_url['path'])) {
	        throw new CException(Nimo::lang('no_dbtype_config'));
	    } else {
	        $db_url['path'] = str_replace('/', '', $db_url['path']);
	    }

	    $config['charset'] = str_replace('-', '', $config['charset']);
	    if (empty($config['charset'])) $config['charset'] = 'utf8';
	    $conn_config = array_merge($config, $db_url);
    	return $this->connect($conn_config);
	}
	
	/**
     * 连接数据库方法
     * @access public
     * @param array $config
     *   $config['host']   数据库主机名
     *   $config['user']   数据库用户名
     *   $config['pw']     数据库密码
     *   $config['path']   数据库名
     *   $config['charset']  数据库字符集
     *   $config['pconnect'] 持久链接,1为开启,0为关闭
     */
    public function connect($config) {
        if ($config['pconnect']) {
    	    $this->_link_id = mssql_pconnect($config['host'], $config['user'], $config['pass']);
        } else {
            $this->_link_id = mssql_connect($config['host'], $config['user'], $config['pass']);
        }
    	if(!$this->_link_id || !mysql_select_db($config['path'], $this->_link_id)) {
            throw new CDbException(mssql_get_last_message());
        }
        return $this->_link_id;
    }
    
    public function query($sql)
    {
        $this->_query_count++;
        if (!($query = mssql_query($sql, $this->_link_id))) {
            throw new CDbException(mssql_get_last_message());
        }
        return $query;
    }
    
    function getAll($sql)
    {
        $res = $this->query($sql);
        if ($res !== false) {
            $arr = array();
            while ($row = mssql_fetch_assoc($res)) {
                $arr[] = $row;
            }
            return $arr;
        } else {
            return false;
        }
    }
    
    /**
	* 取得结果集数组形式
	* @access public
	* @param  $query
	* @return array
	*/
    public function fetch_array($query, $result_type = MYSQL_ASSOC)
    {
	    return mssql_fetch_array($query,$result_type);
	}
	
	/**
	 * 取得结果集对象形式
	 * @access public 
	 * @param  $query
	 */
	public function fetch_fields($query)
	{
	    return mssql_fetch_field($query);
	}
	
	/**
	 * 获取操作影响的行数
	 * @param  $query
	 * @return int 
	 */
	public function affected_rows()
	{
	    return mssql_rows_affected($this->_link_id);
	}
	
	/**
	 * 取得结果集的行数
	 * @param  $query
	 * @return int 
	 */
	public function num_rows($query)
    {
        return mssql_num_rows($query);
    }
    
    /**
	 * 取得结果集中字段的个数
	 * @param  $query
	 * @return int 
	 */
    public function num_fields($query)
    {
        return mssql_num_fields($query);
    }
    
	/**
	* 取得最后一次执行的ID
	* @access public
	* @return int
	*/
    public function insert_id(){
		$query = $this->query("SELECT @@IDENTITY as last_insert_id");
		$row = $this->fetch_array($query);
		$this->free_result($query);
		return $row['last_insert_id'];
    }
    
    public function free_result($query)
    {
        if (mssql_free_result($query)) {
            throw new CDbException(mssql_get_last_message());
        }
    }
    
    /**
	* 获得数据库版本
	* @access public
	* @return string
	*/
    public function version() {
    	return $this->version;
    }

	/** 
	 * 关闭链接
	 * @access public
	 * @return void
	 */
	public function close() {
		if (!empty ($this->queryID))
			mssql_free_result($this->queryID);
		if ($this->_link_id && !mssql_close($this->_link_id)) {
			throw new CDbException(mssql_get_last_message());
		}
		$this->_link_id = null;
	}
}